cd ${work}5_reg_analysis

*** CONSTRUCT TABLE A15
*(Table A13 is constructed manually based on output\table_a13.tex and the tabulation in the last line of this do-file)


use input\sample_ipt, clear
destring jurnr, gen(jurnr_num)

*MERGE ON OUTCOMES FROM DE LOECKER ET AL. PROCEDURE
merge 1:1 jurnr_num vnr unit year using input\vars_ipt, keep(3) keepusing(sales log_mu exit_x section theta_*) nogen

*EXCLUDE SMALL FLOWS
keep if sales>7.5

*EXCLUDE OUTLIERS OF ESTIMATED MARKUPS (FOLLOWING DE LOECKER ET AL.)
keep if exit_x>0 | exit_x==.
drop exit_x
winsor2 log_mu, cuts(3 97) trim replace
keep if log_mu!=.

*PREPARE
gen mu=exp(log_mu)
keep jurnr vnr unit year section theta_* mu

winsor2 theta_* mu, cuts(1 99) replace

gen rts=theta_m+theta_l+theta_k
lab var rts	"Returns to Scale"

*AVERAGES AND STANDARD DEVIATION BY SECTION
eststo clear
gen x=.
global x "theta_l theta_m theta_k rts mu"
levelsof section, local(list)
local option replace
foreach l of local list {
foreach x of global x {
	replace x=`x'
	eststo est_`x'_`l': qui estpost sum x if section==`l'
}
esttab est_*_`l' using output\table_a13.tex, main(mean 2) aux(sd 2) fragment `option' ///
	nostar unstack noobs nonote nomtitle nonumber ///
	substitute(" &" "" "x" "" "\hline" "")
local option append	
}
foreach x of global x {
	replace x=`x'
	eststo est_`x'_all: qui estpost sum x
}
esttab est_*_all using output\table_a13.tex, main(mean 2) aux(sd 2) fragment `option' ///
	nostar unstack noobs nonote nomtitle nonumber ///
	substitute(" &" "" "x" "" "\hline" "")
drop x

*NUMBER OF OBSERVATIONS BY SECTION
tab section